// pages/clerkOrderList/clearkOrderList.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index1: 0,
    option1: ['已预约', '已取货', '待付款', '已付款', '已完成', '已收货', '已评价'],
    src: '',//二维码图片地址
    image: '',
    showModel: false,
    list: [],
    baseImgPath: app.globalData.baseImgPath,
    showPjInput: false,//显示评价提交框
    currentTab: "order",
    current: "1",
    page:1,//当前页数
    showend: false//是否显示无更多数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.current != undefined) {
      that.setData({
        current: options.current
      })
    }
    let s=1;
    console.log(options.current)
    if(options.current){
      s=parseInt(options.current);
    }else{
      s=1
    }
    wx.getStorage({
      key: 'sessionId',
      success: function (res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        wx.request({
          url: app.globalData.urlPath + 'ManagerMent/expressList',
          data: {
            sessionid: that.data.sessionId,
            // id: that.data.id,
            status:s,
            page:that.data.page
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              var data = res.data.data;
              data=data.map(function(item){
                item.createtime = that.totime(item.createtime)
                return item
              })
              that.setData({
                list: data
              })
            }
            else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000,
                mask: true
              })
            }
          }
        })
      },
    })
  },
  //底部导航切换
  handleChangeTab({ detail }) {
    var that=this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.redirectTo({
        url: '/pages/clerkSubOrder/clerkSubOrder',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          wx.showModal({
            title: '是否确认收货?',
            // content: '模态弹窗',
            success: function (res) {
              if (res.confirm) {
                //加载二维码返回数据
                wx.request({
                  url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
                  data: {
                    sessionid: app.globalData.sessionid,
                    qrcode: that.data.qrcode
                  },
                  method: "POST",
                  success: function (res) {
                    console.log(res)
                    if (res.data.code == 200) {
                      wx.showToast({
                        title: res.data.data,
                        icon: 'success',
                        duration: 2000
                      })
                      setTimeout(function () {
                        wx.redirectTo({
                          url:"/pages/clearkOrderList/clearkOrderList?current=5",
                        })
                      })
                    } else {
                      wx.showToast({
                        title: res.data.error,
                        icon: 'none',
                        duration: 2000
                      })
                      if (res.data.code == 5000) {
                        wx.redirectTo({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  }
                })
              } else {
                return;
              }

            }
          })
        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/clerkMy/clerkMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  //导航切换
    handleChange({ detail }) {
      var that=this;
      this.setData({
        current: detail.key
      });
      console.log(detail)
      //根据传的status查询对应的列表
      wx.request({
        url: app.globalData.urlPath + 'ManagerMent/expressList',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: {
          sessionid: that.data.sessionId,
          status: detail.key
        },
        method: "POST",
        success: function (res) {
          console.log(res)
          if (res.data.code == 200) {
            that.setData({
              list:res.data.data
            })
          }
          if (res.data.code == 400) {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000,
              mask: true
            })
            that.setData({
              list: ''
            })
          }
        }
      });
    },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this;
    self.data.page+=1;
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/expressList',
      data: {
        sessionid: self.data.sessionId,
        // id: that.data.id,
        status: self.data.current,
        page: self.data.page
      },
      method: "POST",
      success: function (res) {
         console.log(res.data.data)
        let list = res.data.data;
        if(list){
          list = list.map(function (item) {
            item.createtime = self.totime(item.createtime)
            return item
          })
        }
        if (res.data.code==400) {
          self.setData({ showend: true });
          return false
        }
        self.setData({ list: self.data.list.concat(list) })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //打印二维码
  printCode:function(e){
    var that=this;
    console.log(e)
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/printwq',
      data: {
        sessionid: that.data.sessionId,
        ftid: e.target.id
      },
      method: "POST",
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
          if (res.data.code == 5000) {
             wx.redirectTo({
              url: '/pages/login/login',
            })
          }
        }
      }
    })
  },
  totime: function (t) {
    let T = new Date(t * 1000)
    let text = T.getFullYear() + "/" + (T.getMonth() + 1) + "/" + T.getDate() + "  " + T.getHours() + ":" + T.getMinutes();
    return text;
  }
  
})